package com.qiaoan.back.controller.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiaoan.back.controller.BaseController;
import com.qiaoan.back.enums.ResultCode;
import com.qiaoan.back.model.sys.MenuModel;
import com.qiaoan.back.service.sys.MenuService;
import com.qiaoan.constant.StringConstant;
import com.qiaoan.redis.util.RedisUtil;
import com.qiaoan.utils.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("/sysmenu")
public class MenuController extends BaseController{
	
	@Autowired
	private MenuService service ;

	@Autowired
	private RedisUtil redisUtil ;

	@RequestMapping(value = "/index", method = { RequestMethod.GET })
	public String list(Model model, HttpServletRequest request,
					   HttpServletResponse response) throws Exception {

        //获取父菜单
		MenuModel parentMenu = service.getMenuById(1);
		logger.info("************************************"+ parentMenu.toString());
		model.addAttribute("parentMenu",parentMenu);

		//获取子菜单
		List<MenuModel> secondMenus = service.getchildByPid(parentMenu.getId());
		logger.info("************************************"+ secondMenus.size());
		model.addAttribute("secondMenus",secondMenus);

		//获取全部的菜单
		List<MenuModel> mainTreelists = service.getAllMenus(1);
		logger.info("************************************"+ mainTreelists.size());

		model.addAttribute("mainTreelists",mainTreelists);

		List<MenuModel> lists = service.getMenusList();

		model.addAttribute("lists",lists);

		return "sys/menu/list";

	}



	@RequestMapping(value = "/add", method = { RequestMethod.POST})
	public void add(Model model, HttpServletRequest request,
					HttpServletResponse response,MenuModel menu) throws Exception {

		//
		menu.setEnable(1);
		menu.setLevel(1);
		menu.setCreate_id(1);

		//后台校验
		logger.info("***********************model:"+menu);
		int result = service.insert(menu);
		//处理结果
		if (result > 0){

			printJson(response, ResultCode.Success, StringConstant.INSERT_MSG);
		}else{
			printJson(response,ResultCode.Fail,StringConstant.OPERATE_FAILURE_MSG);
		}

	}

	@RequestMapping(value = "/list", method = { RequestMethod.POST})
	public void getList(HttpServletRequest request,
					HttpServletResponse response,MenuModel menu) throws Exception {

		JSONArray evtArray = new JSONArray();
		List<MenuModel> lists = service.getMenusList();
		for (MenuModel model: lists) {
			JSONObject json  = new JSONObject();
			json.put("id",model.getId());
			json.put("menu_name",model.getMenu_name());
			json.put("create_tima",model.getCreate_time());

			evtArray.add(json);

		}

		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", evtArray);//JSONArray
		jsonObject.put("total",100);//总记录数
		printJson(response,ResultCode.Success,jsonObject.toString());



	}

}
